package mvv.learn.spring.services.accessdata;

import java.util.LinkedList;
import java.util.List;

import mvv.learn.spring.dao.database.EmployeeDao;
import mvv.learn.spring.dto.elasticsearch.ElsEmployeeDto;
import mvv.learn.spring.model.Employee;
import mvv.learn.spring.services.AbsService;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author Manh Vu
 */
@Service
public class EmployeeService extends AbsService {
	@Autowired
	private EmployeeDao employeeDao;

	@Transactional(readOnly = true)
	public List<ElsEmployeeDto> getElsEmployeeDtos(Long employeeId) {
		List<ElsEmployeeDto> list = new LinkedList<>();

		if (employeeId != null) {
			Employee e = employeeDao.findOne(employeeId);
			ElsEmployeeDto ed = new ElsEmployeeDto();
			BeanUtils.copyProperties(e, ed);
			list.add(ed);
		}
		else {
			Iterable<Employee> iter = employeeDao.findAll();
			for (Employee e : iter) {
				ElsEmployeeDto ed = new ElsEmployeeDto();
				BeanUtils.copyProperties(e, ed);

				list.add(ed);
			}
		}

		return list;
	}
}
